:: Copyright (C) 2025 Huawei Device Co., Ltd.
:: Licensed under the Apache License, Version 2.0 (the "License");
:: you may not use this file except in compliance with the License.
:: You may obtain a copy of the License at
::
::     http://www.apache.org/licenses/LICENSE-2.0
::
:: Unless required by applicable law or agreed to in writing, software
:: distributed under the License is distributed on an "AS IS" BASIS,
:: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
:: See the License for the specific language governing permissions and
:: limitations under the License.

@echo off
setlocal enabledelayedexpansion
@call:addPermission

pause
@EXIT /b 0
:addPermission
hdc shell mount -o rw,remount /
hdc target mount

@echo on 
hdc file recv /system/etc/communication/softbus/softbus_trans_permission.json /
@echo off

set "tempFile=temp.json"
set "targetsFile=softbus_trans_permission.json"

rem 判断目标文件是否存在
if not exist "%targetsFile%" (
   echo %targetsFile% 不存在
   exit /b 1
)

rem 读取目标文件
set "search_string=com.communication.demo*"
set "found=false"
for /f "usebackq delims=" %%a in ("%targetsFile%") do (
    set "line=%%a"
	if not "!line:%search_string%=!" == "!line!" (
	    set found=true
		goto :exit_loop
	)
)

set "lineCount=0"
(for /f "usebackq delims=" %%a in ("%targetsFile%") do (
   set /a "lineCount+=1"
   if !lineCount! equ 2 (
      echo   {
      echo     "SESSION_NAME": "com.communication.demo*",
      echo     "REGEXP": "true",
      echo     "DEVID": "UDID",
      echo     "SEC_LEVEL": "public",
      echo     "APP_INFO": [
      echo       {
      echo         "TYPE": "normal_app",
      echo         "PKG_NAME": "com.communication.demo1",
      echo         "ACTIONS": "create,open"
      echo       },
      echo       {
      echo         "TYPE": "normal_app",
      echo         "PKG_NAME": "com.communication.demo.max.len.12345678901234567890123456789012",
      echo         "ACTIONS": "create,open"
      echo       },
      echo       {
      echo         "TYPE": "normal_app",
      echo         "PKG_NAME": "com.communication.demo.max.len.123456789012345678901234567890123",
      echo         "ACTIONS": "create,open"
      echo       },
      echo       {
      echo         "TYPE": "normal_app",
      echo         "PKG_NAME": "com.communication.demo.max.len.#@$%%^!",
      echo         "ACTIONS": "create,open"
      echo       }
	  echo     ]
      echo	  },
	)
	echo.%%a
))>"%tempFile%"

rem 替换文件
move /y "%tempFile%" "data/%targetsFile%"

echo Success
@EXIT /b 0

:exit_loop
if %found%==true (
   echo devices dsoftbus's config is avaliable. push fail
   move /y "%targetsFile%" "data/%targetsFile%"
) else (
   echo String not found in file
)
@EXIT /b 1










	       
